//
//  CTPPlaceholderTextView.m
//  LittleNotes
//
//  Created by 檀志文 on 16/5/19.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "CTPPlaceholderTextView.h"
#import <Masonry.h>

@interface CTPPlaceholderTextView()

//占位label
@property (nonatomic, strong) UILabel *placeholderLabel;


@end

@implementation CTPPlaceholderTextView

// 占位label 懒加载
- (UILabel *)placeholderLabel{
    if (_placeholderLabel == nil) {
        UILabel *placeholderLabel =[[UILabel alloc]init];
        placeholderLabel.font = [UIFont systemFontOfSize:13];
        placeholderLabel.textColor = [UIColor lightGrayColor];
        placeholderLabel.text = @"写点什么吧...";
        [placeholderLabel sizeToFit];
        [self addSubview:placeholderLabel];
        _placeholderLabel = placeholderLabel;
    }
    return _placeholderLabel;
}
//  
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self prepareUI];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didChangeText) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

-(instancetype) initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        
    }
    return self;
}

-(instancetype) initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer{
    if (self = [super initWithFrame:frame textContainer:textContainer]) {
        [self prepareUI];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didChangeText) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}
-(void) dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// 判断是否有值
-(void) didChangeText{
    self.placeholderLabel.hidden = [self hasText];
}

-(void) prepareUI{
    [_placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(5);
        make.top.equalTo(self).offset(8);
    }];
}

- (void)setTextContent:(NSString *)textContent{
    _textContent = textContent;
    _placeholderLabel.hidden = [self hasText];
}




@end
